package stream;

import org.junit.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * 流的创建（4中构建形式）
 * @author zhaowenxu
 * @date 2020/4/7 1:06
 */
public class StreamConstructor {

    /**
     * 数值直接构建流
     */
    @Test
    public void streamFromValue() {
        Stream<Integer> integerStream = Stream.of(1, 2, 3);
        integerStream.forEach(System.out::println);
    }

    /**
     * 通过数组构建流
     */
    @Test
    public void streamFromArray() {
        int[] numberArray = {1, 2, 3, 4};
        IntStream stream = Arrays.stream(numberArray);
        stream.forEach(System.out::println);
    }

    /**
     * 通过文件生成流
     * @throws IOException
     */
    @Test
    public void streamFromFile() throws IOException {
        Stream<String> stream = Files.lines(Paths.get("D:/idea_code/996/src/test/java/stream/StreamConstructor.java"));
        stream.forEach(System.out::println);
    }

    @Test
    public void streamFromFunction() {
        //Stream<Integer> iterate = Stream.iterate(0, n -> n + 2);
        //iterate.forEach(System.out::println);
        Stream<Double> generate = Stream.generate(Math::random);
        generate.limit(100)
                .forEach(System.out::println);
    }
}
